<?php
namespace app\finance\admin;
use app\system\admin\Admin;
use app\product\model\Supplier as SupplierModel;
use app\finance\model\Account as AccountModel;
use think\Db;
class Pay extends Admin
{
    protected $hisiModel = '';//模型名称[通用添加、修改专用]
    protected $hisiTable = 'supplier_money';//表名称[通用添加、修改专用]
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名

    protected function initialize()
    {
        parent::initialize();
        if ($this->request->action() != 'index' && !$this->request->isPost()) {
            $suppliers = SupplierModel::getSelect(SupplierModel::all());
        	$this->assign('suppliers',$suppliers);
        	$accounts = AccountModel::getSelect(AccountModel::where('id','<>',0)->all());
        	$this->assign('accounts',$accounts);
        }
    }

    public function index()
    {
        return $this->fetch();
    }
    public function supplier()
    {
    	if($this->request->isAjax()){
    		$where      = $data = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $bill_id    = $this->request->param('bill_id','');
            $type       = $this->request->param('type',1);

            if ($bill_id) {
                $where['supplier_id'] = $bill_id;
            }
            $where['out_in'] = 1;
            if($type == 2){
                $data['data']   = Db::name('supplier_money')->alias('sm')
                                ->join('hisi_supplier s','s.id=sm.supplier_id')
                                ->join('hisi_account a','a.id=sm.account_id')
                                ->field('sm.*,s.name as supplier_name, a.name as account_name,sm.status')
                                ->where($where)
                                // ->page($page)->limit($limit)
                                ->order('createtime desc')
                                ->select();
                $data['count']  = Db::name('supplier_money')->where($where)->count('id');
                $data['code']   = 0;
                return json($data);
            }else{
                $data['data']   = Db::name('supplier_money')->alias('sm')
    				            ->join('hisi_supplier s','s.id=sm.supplier_id')
    				            ->join('hisi_account a','a.id=sm.account_id')
    				            ->field('sm.*,s.name as supplier_name, a.name as account_name,sm.status')
    				            ->where($where)
                                ->order('createtime desc')
    				            ->page($page)->limit($limit)->select();
                $data['count']  = Db::name('supplier_money')->where($where)->count('id');
                $data['code']   = 0;
                return json($data);
            }

    	}
        $bills = Db::name('supplier')->field('id,name')->all();
		$this->assign('bills',$bills);
    	return $this->fetch();
    }
    public function addSupplier()
    {
    	if($this->request->isPost()){
    		$postData = $this->request->post();
    		$postData['createtime'] = date('Y-m-d H:i:s',time());
    		$postData['sn']  =  create_order_sn();
            $postData['status'] = 0;
            $postData['tj_id'] = ADMIN_ID;
            if(Db::name('supplier_money')->insert($postData)){
                return $this->success('转账申请已提交，请等待审核');
            }
            return $this->error('提交失败！');
            /*
    		// 启动事务
	        Db::startTrans();
	        try {
	    		Db::name('supplier_money')->insert($postData);
                  if($postData['price'] > 0){
                      $ins = [
                          'sn'  => $postData['sn'],
                          'type'  => 5,
                          'account_id'  => $postData['account_id'],
                          'price'  => $postData['price'],
                          'createtime'  => time(),
                          'updatetime'  => time(),
                          'is_effective'  => 1,
                          'cate'  => 2,
                          'bak'   => $postData['bak']
                      ];
                  }else{
                      $ins = [
        					'sn'  => $postData['sn'],
        					'type'  => 7,
        					'account_id'  => $postData['account_id'],
        					'price'  => abs($postData['price']),
        					'createtime'  => time(),
        					'updatetime'  => time(),
        					'is_effective'  => 1,
        					'cate'  => 1,
                            'bak'   => $postData['bak']
            	    		];
                  }

	    		Db::name('system_balance_log')->insert($ins);

	    		// 提交事务
	            Db::commit();
	        } catch (\Exception $e) {
	            // 回滚事务
	            Db::rollback();
	            return $this->axiosMsg($e->getMessage(), false);
	        }
	        return $this->success('转账成功！');
            */
    	}

    	return $this->fetch();
    }
    public function check()
    {
        $id = $this->request->param('id');
        $formData = Db::name('supplier_money')->where('id',$id)->find();
        if($this->request->isPost()){
    		$postData = $this->request->post();
            Db::startTrans();
            try {
                if($formData['tj_id'] == ADMIN_ID){
                    throw new \Exception("提交财务与审核财务不能为同一人", 1);

                }
                $postData['sh_id'] = ADMIN_ID;
                Db::name('supplier_money')->where('id',$id)->update($postData);
                if( $postData['status'] == 1 ){
                    if($postData['price'] > 0){
                        $ins = [
                            'sn'  => $formData['sn'],
                            'type'  => 5,
                            'account_id'  => $postData['account_id'],
                            'price'  => $postData['price'],
                            'createtime'  => time(),
                            'updatetime'  => time(),
                            'is_effective'  => 1,
                            'cate'  => 2,
                            'bak'   => $postData['bak']
                        ];
                    }else{
                        $ins = [
                              'sn'  => $formData['sn'],
                              'type'  => 7,
                              'account_id'  => $postData['account_id'],
                              'price'  => abs($postData['price']),
                              'createtime'  => time(),
                              'updatetime'  => time(),
                              'is_effective'  => 1,
                              'cate'  => 1,
                              'bak'   => $postData['bak']
                         ];
                    }

                  Db::name('system_balance_log')->insert($ins);
                }


                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return $this->error($e->getMessage());
            }
            return $this->success('审核完成！');
        }

        $this->assign('formData',$formData);
        $suppliers = SupplierModel::getSelect(SupplierModel::all());
    	$this->assign('suppliers',$suppliers);
    	$accounts = AccountModel::getSelect(AccountModel::where('id','<>',0)->all());
    	$this->assign('accounts',$accounts);
        return $this->fetch();
    }

    public function getAccountBalance()
    {
        $id = $this->request->param('account_id');
        $AccountModel = new AccountModel;
        $balance = $AccountModel->getAccountBalance($id);
        echo $balance;die;
    }
}
